#version 460

layout (location = 0 ) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in vec2 VertexTexCoord;

out vec4 Position;
out vec3 Normal;
out vec2 TexCoord;

uniform float Time;
uniform float Freq = 2.5f;
uniform float Velocity = 2.5f;
uniform float Amp = 0.6f;
// noise
uniform float noiseTilling = 1.0f;
uniform float noiseSpeed = 1.0f;
uniform float noiseSacle = 0.5f;

uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 MVP;

// 2D Random
float random (in vec2 st) 
{
    return fract(sin(dot(st.xy,
                         vec2(12.9898,78.233)))
                 * 43758.5453123);
}

float noiseFunc (in vec2 st) 
{
    vec2 i = floor(st);
    vec2 f = fract(st);

    float a = random(i);
    float b = random(i + vec2(1.0, 0.0));
    float c = random(i + vec2(0.0, 1.0));
    float d = random(i + vec2(1.0, 1.0));


    // Cubic Hermine Curve.  Same as SmoothStep()
    vec2 u = f*f*(3.0-2.0*f);
    u = smoothstep(0.,1.,f);

    return mix(a, b, u.x) +
            (c - a)* u.y * (1.0 - u.x) +
            (d - b) * u.x * u.y;
}
void main()
{
    vec4 pos = vec4(VertexPosition,1.0);

    float u = Freq * pos.x - Velocity * Time;
    pos.y = Amp * sin( u );
	vec2 noiseUV = VertexTexCoord * noiseTilling * Time * noiseSpeed;
    float noise = noiseFunc(noiseUV);
	pos.y += noise * noiseSacle;
    vec3 n = vec3(0.0);
    n.xy = normalize(vec2(cos( u ), 1.0));

    Position = ModelViewMatrix * pos;
    Normal = NormalMatrix * n;
    TexCoord = VertexTexCoord;
    gl_Position = MVP * pos;
}
